home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 8- transitions / source / animalpane.java next >
Encoding:
Java Source  |  2000-06-23  |  15.0 KB  |  488 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4. import java.util.Hashtable;
  5.  
  6. import quicktime.app.QTFactory;
  7. import quicktime.app.anim.Compositor;
  8. import quicktime.app.anim.TwoDSprite;
  9. import quicktime.app.anim.SWController;
  10. import quicktime.app.event.QTActionEvent;  
  11. import quicktime.app.event.QTActionListener;
  12. import quicktime.app.event.QTMouseTargetController; 
  13. import quicktime.app.image.GraphicsImporterDrawer; 
  14. import quicktime.app.image.ImagePresenter; 
  15. import quicktime.app.image.ImageDataSequence;
  16. import quicktime.app.image.ImageUtil;
  17. import quicktime.app.ui.ReleaseButton;
  18. import quicktime.app.ui.ButtonActivator;
  19. import quicktime.app.players.MoviePresenter;
  20. import quicktime.app.players.QTPlayer;
  21.  
  22. import quicktime.io.QTFile;
  23. import quicktime.io.OpenMovieFile;
  24.  
  25. import quicktime.qd.QDRect;
  26. import quicktime.qd.QDGraphics;
  27. import quicktime.qd.QDColor; 
  28. import quicktime.qd.QDConstants; 
  29.  
  30. import quicktime.std.StdQTConstants;
  31. import quicktime.std.movies.Movie; 
  32. import quicktime.std.image.Matrix;
  33. import quicktime.std.image.GraphicsMode;
  34.  
  35. import quicktime.QTSession;
  36. import quicktime.QTException; 
  37.  
  38. /**
  39.  * QTZoo Module 6 - Advanced Compositing and Event Handling
  40.  * This application requires QuickTime for Java
  41.  *
  42.  * @author Levi Brown
  43.  * @author Michael Hopkins
  44.  * @author Apple Computer, Inc.
  45.  * @version 6.0 4/10/2000
  46.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  47.  *    
  48.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  49.  *                ("Apple") in consideration of your agreement to the following terms, and your
  50.  *                use, installation, modification or redistribution of this Apple software
  51.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  52.  *                please do not use, install, modify or redistribute this Apple software.
  53.  *
  54.  *                In consideration of your agreement to abide by the following terms, and subject
  55.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  56.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  57.  *                reproduce, modify and redistribute the Apple Software, with or without
  58.  *                modifications, in source and/or binary forms; provided that if you redistribute
  59.  *                the Apple Software in its entirety and without modifications, you must retain
  60.  *                this notice and the following text and disclaimers in all such redistributions of
  61.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  62.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  63.  *                Apple Software without specific prior written permission from Apple.  Except as
  64.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  65.  *                are granted by Apple herein, including but not limited to any patent rights that
  66.  *                may be infringed by your derivative works or by other works in which the Apple
  67.  *                Software may be incorporated.
  68.  *
  69.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  70.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  71.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  72.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  73.  *                COMBINATION WITH YOUR PRODUCTS.
  74.  *
  75.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  76.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  77.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  78.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  79.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  80.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  81.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  82.  * 
  83.  * 
  84.  */
  85.  
  86. public class AnimalPane extends ZooPane
  87. {
  88.     /**
  89.      *  Public default constructor
  90.      *  Creates the map button, and sets up the listeners
  91.      */
  92.     public AnimalPane()
  93.     {
  94.         QDRect size = new QDRect(MainFrame.WIDTH, MainFrame.HEIGHT);
  95.         try
  96.         {
  97.             loadSound("data/zebra/Zebra.au");
  98.  
  99.             QDGraphics gw = new QDGraphics (size);
  100.             compositor = new Compositor (gw, QDColor.white, 30, 1);
  101.             
  102.             displayImage( "data/zebra/ZebraBackground.jpg", 6, 5, 110 );        
  103.                         
  104.             addText( "data/zebra/Zebra.txt", 5, 15, 65, 415, 220 );
  105.             
  106.             addMovie( "data/zebra/Zebra.mov", 2, 425, 65 );
  107.  
  108.             ctr = new QTMouseTargetController( false );
  109.             ctr.addQTMouseListener( new PaneMouseListener( QDColor.lightGray, QDColor.white, QDColor.darkGray));
  110.             compositor.addController( ctr );
  111.             
  112.             currentSprite = CreateRollover( "data/MapButton.jpg", 5, 12, 406 );
  113.  
  114.              areas = new Hashtable(1);            
  115.              areas.put(currentSprite, "map");        
  116.         }
  117.         catch ( QTException qte )
  118.         {
  119.             qte.printStackTrace();
  120.         }
  121.     }    
  122.     
  123.     /**
  124.      * Listener object for the Movie controller buttons
  125.      * Called when the button is released
  126.      */
  127.     public class ButtonListener implements QTActionListener    
  128.     {
  129.         /**
  130.          * Called when the button is released
  131.          * @param ???
  132.          */
  133.         public void actionPerformed( QTActionEvent event ) 
  134.         {
  135.             try
  136.             {
  137.                 Object source = event.getSource();
  138.                 
  139.                 if (source.equals(playButton))            // Play >
  140.                 {
  141.                     if ( md.getRate() == 0 )
  142.                     {
  143.                         md.setRate( 1 );
  144.                     }
  145.                     stopButton.setCurrentImage( stopRel );
  146.                     stopButton.setReleasedImage( stopRel );
  147.                     playButton.setCurrentImage( playPlaying );
  148.                     playButton.setReleasedImage( playPlaying );
  149.                 }                
  150.                 else if (source.equals(stopButton))        // Stop []
  151.                 {
  152.                     if ( md.getRate() == 1 )
  153.                     {
  154.                         md.setRate( 0 );
  155.                     }
  156.                     stopButton.setCurrentImage( stopDeactive );
  157.                     stopButton.setReleasedImage( stopDeactive );
  158.                     playButton.setCurrentImage( playRel );
  159.                     playButton.setReleasedImage( playRel );
  160.                 }
  161.                 else if (source.equals(rewindButton))    // Rewind <<
  162.                 {
  163.                     md.setTime(0);
  164.                 }
  165.             }
  166.             catch (QTException exc )
  167.             {
  168.                 exc.printStackTrace();
  169.             }
  170.         }
  171.     }
  172.     
  173.     /**
  174.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  175.      * @param ???
  176.      */
  177.     public ImagePresenter MakePresenterFromFile( String name )
  178.     {
  179.         try
  180.         {
  181.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  182.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  183.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  184.         }
  185.         catch (IOException e)
  186.         {
  187.             e.printStackTrace();
  188.         }
  189.         catch (QTException e)
  190.         {
  191.             e.printStackTrace();
  192.         }
  193.         return null;    
  194.     }
  195.     
  196.     /**
  197.      * Called to do any setup after being set as the client of the QTCanvas.
  198.      * May be used to start effects running, movies playing, etc.
  199.      */
  200.     public void start()
  201.     {
  202.         buttonController.addQTMouseListener(buttonActivator);
  203.         try
  204.         {
  205.             stopButton.setCurrentImage(  stopRel );         // Stop button should not have focus
  206.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  207.             playButton.setReleasedImage( playPlaying );
  208.         }
  209.         catch( QTException e )
  210.         {
  211.             e.printStackTrace();
  212.         }
  213.         try
  214.         {
  215.             md.setRate(1);                                // start the player
  216.         }
  217.         catch( QTException e )
  218.         {
  219.             e.printStackTrace();
  220.         }
  221.         compositor.getTimer().setRate(1);                // start the compositor
  222.     }
  223.     
  224.     /**
  225.      * Called to do clean up after being removed as the client of the QTCanvas.
  226.      * Should be used to stop effects running, movies playing, etc.
  227.      */
  228.     public void stop()
  229.     {
  230.         buttonController.removeQTMouseListener(buttonActivator);
  231.         compositor.getTimer().setRate(0);
  232.         try
  233.         {
  234.             md.setRate(0);
  235.         }
  236.         catch( QTException e )
  237.         {
  238.             e.printStackTrace();
  239.         }
  240.     }
  241.     
  242.     /**
  243.      *  Plays the sound file associated with the current AnimalPane
  244.      */
  245.     public void playSound()
  246.     {
  247.         if (player == null)
  248.             return;
  249.  
  250.         try
  251.         {
  252.             player.setTime(0);            //Start the sound at the beginning
  253.             player.startTasking();         //Make sure the player gets time to play the sound
  254.             player.setRate(1);             //Start playing
  255.         }
  256.         catch (QTException exc)
  257.         {
  258.             exc.printStackTrace();
  259.         }
  260.     }
  261.     
  262.     /**
  263.      * Creates a Movie from the specified path and adds it
  264.      * to the compositor in the hardcoded location.
  265.      * @param moviePath the relative path to the movie file to add.
  266.      * @param layer, the layer of the compositor to add the movie to.
  267.      * @param x the x coordinate location.
  268.      * @param y the y coordinate location.
  269.      * @see #makeMovie
  270.      */
  271.     protected void addMovie(String moviePath, int layer, int x, int y)
  272.     {
  273.         try
  274.         {
  275.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  276.             md = new MoviePresenter (m);
  277.             md.setLocation (x, y);
  278.  
  279.             playRel     = MakePresenterFromFile( "data/Play.jpg" );            // Play >
  280.             playPress   = MakePresenterFromFile( "data/PlayPressed.jpg" );
  281.             playPlaying = MakePresenterFromFile( "data/Playing.jpg" );
  282.             playButton = new MovieButton( playRel, playPress, playPlaying );
  283.             playButton.setLabel ("Play");
  284.             playButton.setLocation( x + 68, y + 152 );
  285.             
  286.             stopRel      = MakePresenterFromFile( "data/Stop.jpg" );        // Stop []
  287.             stopPress    = MakePresenterFromFile( "data/StopPressed.jpg" );
  288.             stopDeactive = MakePresenterFromFile( "data/Stopped.jpg" );
  289.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  290.             stopButton.setLabel ("Stop");
  291.             stopButton.setLocation( x + 136, y + 152 );
  292.             
  293.             rewRel      = MakePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  294.             rewPress    = MakePresenterFromFile( "data/RewindPressed.jpg" );
  295.             rewDeactive = MakePresenterFromFile( "data/Rewind.jpg" );
  296.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  297.             rewindButton.setLabel( "Rewind" );
  298.             rewindButton.setLocation( x, y + 152 );
  299.             
  300.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  301.             
  302.             playButton.addActionListener(bl);
  303.             stopButton.addActionListener(bl);
  304.             rewindButton.addActionListener(bl);
  305.             
  306.             compositor.addMember (md, layer);                                // add the movie presenter
  307.             compositor.addMember (playButton);                                //     and buttons to the compositor
  308.             compositor.addMember (stopButton);
  309.             compositor.addMember (rewindButton);
  310.             
  311.             buttonController = new QTMouseTargetController (false);    
  312.             buttonController.addMember(playButton);
  313.             buttonController.addMember(stopButton);
  314.             buttonController.addMember(rewindButton);
  315.         
  316.             compositor.addController (buttonController);
  317.             buttonActivator = new ButtonActivator();
  318.             
  319.             md.setRate(1);                                                    // start the movie        
  320.         }
  321.         catch (QTException exc)
  322.         {
  323.             exc.printStackTrace();
  324.         }
  325.         catch ( IOException exc )
  326.         {
  327.             exc.printStackTrace();
  328.         }    
  329.     }
  330.     
  331.     /**
  332.      * Utility routine loads an image from a file, renders it, and adds it to the compositor
  333.      * @param filename the name of the file to be displayed
  334.      * @param layer the layer in the compositor where the image will be displayed
  335.      * @param x the x coordinate of the top left corner where the image is to be displayed
  336.      * @param y the y coordinate of the top left corner where the image is to be displayed
  337.      */
  338.     protected void displayImage( String filename, int layer, int x, int y )
  339.     {
  340.         try
  341.         {    
  342.             QTFile file = new QTFile (QTFactory.findAbsolutePath (filename));
  343.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  344.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  345.             presenter.setLocation( x, y );
  346.             compositor.addMember(presenter, layer );
  347.         }
  348.         catch (QTException exc)
  349.         {
  350.             exc.printStackTrace();
  351.         }
  352.         catch (IOException exc)
  353.         {
  354.             exc.printStackTrace();
  355.         }        
  356.     }
  357.     
  358.     /**
  359.      * Reads a text file from the disk and displays it in the compositor
  360.      * @param textPath the relative path where the text is located
  361.      * @param layer, the layer of the compositor to add the text to.
  362.      * @param x the x coordinate location.
  363.      * @param y the y coordinate location.
  364.      * @param width the width of the area to display the text.
  365.      * @param height the height of the area to display the text.
  366.      */
  367.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  368.     {
  369.         try
  370.         {
  371.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  372.             
  373.             ImagePresenter presenter = text.getPresenter();
  374.             Matrix theMatrix = new Matrix();
  375.             theMatrix.translate( (float) x, (float) y );
  376.             presenter.setMatrix(theMatrix);
  377.             compositor.addMember(presenter, layer);
  378.         }
  379.         catch ( QTException exc )
  380.         {
  381.             exc.printStackTrace();
  382.         }
  383.         catch ( FileNotFoundException exc )
  384.         {
  385.             exc.printStackTrace();
  386.         }    
  387.     }
  388.     
  389.     /**
  390.      * Loads the specified sound file from disk and prepares it for playing
  391.      * @param ???
  392.      */
  393.     protected void loadSound(String soundPath)
  394.     {
  395.         try
  396.         {
  397.             String soundLocation = QTFactory.findAbsolutePath(soundPath).getPath();
  398.             //this call works with a file://, http://, rtsp://located movie
  399.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  400.         }
  401.         catch (IOException exc)
  402.         {
  403.             exc.printStackTrace();
  404.         }
  405.         catch (QTException exc)
  406.         {
  407.             exc.printStackTrace();
  408.         }
  409.     }
  410.     
  411.  
  412.     /**
  413.      * Opens the movie file and sets it up to be played.
  414.      * @param f a QTFile representing the movie to initialize.
  415.      * @return the movie, ready to play
  416.      * @see #addMovie
  417.      */
  418.     protected Movie makeMovie (QTFile f) throws IOException, QTException
  419.     {
  420.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  421.         Movie m = Movie.fromFile (movieFile);
  422.         m.getTimeBase().setFlags (StdQTConstants.loopTimeBase);    
  423.         return m;    
  424.     }
  425.     
  426.     /**
  427.      *    Creates a TwoDSprite that behaves as a simple rollover button (such as the map button)
  428.      */
  429.     protected TwoDSprite CreateRollover( String filename, int layer, int x, int y )
  430.     {
  431.         TwoDSprite sprite = null;
  432.         try
  433.         {
  434.             QTFile                 file = new QTFile( QTFactory.findAbsolutePath( filename ));
  435.             ImagePresenter         presenter = ImagePresenter.fromFile( file );
  436.             ImageDataSequence     dataSeq = new ImageDataSequence (presenter.getDescription());
  437.             
  438.             dataSeq.addMember( presenter.getImage() );
  439.             
  440.             if ((QTSession.isCurrentOS(QTSession.kWin32) && QTSession.getQTMajorVersion() == 3) == false)    //doesn't work on QT3.0.2 on Win
  441.                 dataSeq = ImageUtil.makeTransparent ( dataSeq, QDColor.white );        // recompress the image using white as the transparent color
  442.             
  443.             Matrix theMatrix = new Matrix();
  444.             theMatrix.translate( (float) x, (float) y );
  445.             sprite = new TwoDSprite( dataSeq, theMatrix, true, 10 );
  446.             
  447.             GraphicsMode normalGM = new GraphicsMode (QDConstants.blend, QDColor.lightGray);
  448.             sprite.setGraphicsMode (normalGM);
  449.             compositor.addMember(sprite, layer);
  450.             ctr.addMember( sprite );
  451.         }
  452.         catch ( QTException exc )
  453.         {
  454.             exc.printStackTrace();
  455.         }
  456.         catch ( IOException exc )
  457.         {
  458.             exc.printStackTrace();
  459.         }
  460.         
  461.         return sprite;    
  462.     }
  463.     
  464.     
  465.     protected QTPlayer player;
  466.     protected MoviePresenter md;
  467.     
  468.     protected ImagePresenter playRel;
  469.     protected ImagePresenter playPress;
  470.     protected ImagePresenter playPlaying;
  471.     protected MovieButton playButton;
  472.             
  473.     protected ImagePresenter stopRel;
  474.     protected ImagePresenter stopPress;
  475.     protected ImagePresenter stopDeactive;
  476.     protected MovieButton stopButton;
  477.     
  478.     protected ImagePresenter rewRel;
  479.     protected ImagePresenter rewPress;
  480.     protected ImagePresenter rewDeactive;
  481.     protected MovieButton rewindButton;
  482.     
  483.     protected TwoDSprite currentSprite;
  484.     
  485.     protected QTMouseTargetController buttonController;
  486.     protected ButtonActivator buttonActivator;
  487. }
  488.